import java.awt.*; import java.awt.event.*; import javax.swing.*; class MyFrame extends Frame implements WindowListener { String msg="Good Bye"; public MyFrame() { this.addWindowListener(this); } public void windowActivated(WindowEvent e){} public void windowDeactivated(WindowEvent e){} public void windowClosing(WindowEvent e) { JOptionPane.showMessageDialog(null,msg); System.exit(0); } public void windowClosed(WindowEvent e){} public void windowIconified(WindowEvent e){} public void windowDeiconified(WindowEvent e){} public void windowOpened(WindowEvent e){} } class demo { public static void main(String args[ ]) { MyFrame frm=new MyFrame(); frm.setSize(500,300); frm.setVisible(true); } }
Adapter | Listener interface |
---|---|
WindowAdapter | WindowListener |
KeyAdapter | KeyListener |
MouseAdapter | MouseListener |
MouseMotionAdapter | MouseMotionListener |
FocusAdapter | FocusListener |
ComponentAdapter | ComponentListener |
ContainerAdapter | ContainerListener |
import java.awt.*; import java.awt.event.*; class MyFrame extends Frame { Label lbl; public MyFrame() { lbl=new Label(); add(lbl,"South"); lbl.setBackground(Color.cyan); MyMouseHandler x=new MyMouseHandler(); this.addMouseMotionListener(x); MyWinHandler y=new MyWinHandler(); this.addWindowListener(y); } class MyMouseHandler extends MouseMotionAdapter { public void mouseMoved(MouseEvent e) { int x=e.getX(); int y=e.getY(); String msg="X="+x+" Y="+y; lbl.setText(msg); } } class MyWinHandler extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } } class demo { public static void main(String args[]) { MyFrame frm=new MyFrame(); frm.setSize(500,300); frm.setVisible(true); } }